package com.yricky.tsp;


import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.VectorDrawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.view.menu.MenuBuilder;


public class MainActivity extends AppCompatActivity {

    private EditText editTextIp;
    private EditText editTextCp;
    private SeekBar seekBarMr;
    private TextView textViewMr;
    private Button buttonNavStart;
    private PaintLayout paintLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editTextIp = findViewById(R.id.editTextNumber_ip);
        editTextCp=findViewById(R.id.editTextNumber_cp);
        seekBarMr=findViewById(R.id.seekBar_mr);
        textViewMr=findViewById(R.id.nav_mr_text);
        buttonNavStart=findViewById(R.id.nav_button_start);
        paintLayout = findViewById(R.id.paintView);
        seekBarMr.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                textViewMr.setText(""+1.0*progress/100);
                applyParams();
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        editTextCp.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                applyParams();
            }
        });
        paintLayout.setOnCityAddListener(new PaintLayout.OnCityAddListener() {
            @Override
            public void onAdd(int cityNum) {
                editTextCp.setText(""+(cityNum+5));
                editTextIp.setText(""+(cityNum+5));
            }
        });

        buttonNavStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(paintLayout.isRunning()){
                    paintLayout.stopSolving();
                    editTextIp.setEnabled(true);
                    buttonNavStart.setText(R.string.start);
                }else if(paintLayout.solveReady()&&applyParams()){

                    paintLayout.startSolving();
                    editTextIp.setEnabled(false);
                    buttonNavStart.setText(R.string.stop);
                }
            }
        });
    }
    private boolean applyParams(){
        int cp,ip;
        try {
            cp=Integer.parseInt(editTextCp.getText().toString());
            ip=Integer.parseInt(editTextIp.getText().toString());
        }catch (NumberFormatException e){
            return false;
        }

        if(cp>0&&ip>0) {
            paintLayout.setSolveCP(cp);
            paintLayout.setSolveIP(ip);
            paintLayout.setSolveMR(seekBarMr.getProgress() * 1.0 / 100);
            return true;
        }
        return false;
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(1,1,1,R.string.reset);
        menu.add(1,2,2,"demo");
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case 1:
                paintLayout.stopSolving();
                editTextIp.setEnabled(true);
                paintLayout.reset();
                editTextCp.setText("5");
                editTextIp.setText("5");
                break;
            case 2:
                editTextIp.setEnabled(true);
                paintLayout.createCityDemo(100);
                break;
            case 3:


        }
        return super.onOptionsItemSelected(item);
    }


}